package com.hhu.utils;

import android.util.Log;

import com.google.gson.Gson;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.HashMap;
import java.util.Map;

public class THhuMqtt {
    String svrUrl="";
    String uid="";
    String pwd="";
    String clientId="";
    String clientCode="";
    MqttClient client ;
    THhuMqttEvents events;
    public THhuMqtt(
            String _srvUrl ,
            String _uid ,
            String _pwd ,
            String _clientCode ,
            THhuMqttEvents _events
    ){
        svrUrl = _srvUrl;
        uid = _uid;
        pwd = _pwd;
        clientCode = _clientCode;
        clientId = TComm.getNewId();
        events = _events;
        if( events==null){
            events = new THhuMqttEvents(){};
        }
    }
    public  void open(){
        try{
            close();
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(false);
            options.setUserName(uid);
            options.setPassword(pwd.toCharArray());
            options.setConnectionTimeout(10);
            options.setKeepAliveInterval(40);
            client = new MqttClient( svrUrl , clientId , new MemoryPersistence());
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    System.out.println("连接断开，可以做重连");
                    try{
                        events.onDisConnected();
                    }
                    catch (Exception erE){
                        ;
                    }
                }

                @Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    String msgContent = new String(message.getPayload());
                    Map<String , String> msgMap = new HashMap<String, String>();
                    try{
                        msgMap = ( new Gson()).fromJson(msgContent , msgMap.getClass());
                    }
                    catch (Exception eer){
                        ;
                    }
                    events.onReceiveMsg(topic , msgContent , msgMap);
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("deliveryComplete---------" + token.isComplete());
                }
            });
            client.connect(options);
            if( client.isConnected()){
                try{
                    String[] topics =new String[]{
                            "hh/all/#",
                            "hh/client/"+clientId+"/#",
                            "hh/client/"+clientCode+"/#"
                    };
                    client.subscribe(topics);
                    events.onConnected();
                }
                catch (Exception erE){
                    ;
                }
            }
            else {
                events.onDisConnected();
            }
        }
        catch (Exception er){
            ;
        }
    }
    public  void close(){
        try{
            if( client!=null){
                if( client.isConnected()){
                    client.close();
                }
                client = null;
            }
        }
        catch (Exception er){
            ;
        }
    }
    public  void sendMsg( String topic , String msgContent ){
        try{

        }
        catch (Exception er){
            ;
        }
    }
    public  void sendMsg( String topic , Map<String , String> msg ){
        try{

        }
        catch (Exception er){
            ;
        }
    }
    public MqttClient getClient(){
        return  client;
    }

    public static class  THhuMqttEvents{
        public void onConnected (){}
        public void onDisConnected (){}
        public void onReceiveMsg ( String topic , String msgContent , Map<String , String> msgInfo){}
    }
}
